package com.ktwlrj.securitycenter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktwlrj.core.constant.ResultCode;
import com.ktwlrj.core.exception.ActionException;
import com.ktwlrj.core.util.ContextHolderUtils;
import com.ktwlrj.core.util.convert.ConvertUtil;
import com.ktwlrj.securitycenter.controller.args.SafeTriggerLogArgs;
import com.ktwlrj.securitycenter.controller.args.SafeTriggerLogPageArgs;
import com.ktwlrj.securitycenter.dto.SafeTriggerLogPageDto;
import com.ktwlrj.securitycenter.entity.SafeTriggerLogEntity;
import com.ktwlrj.securitycenter.mapper.SafeTriggerLogMapper;
import com.ktwlrj.securitycenter.service.SafeTriggerLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.stereotype.Service;

/**
 * 触发日志业务层实现
 *
 * @author code generation
 * @date 2020-02-24 18:08:14
 * @since JDK1.8
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SafeTriggerLogServiceImpl extends ServiceImpl<SafeTriggerLogMapper, SafeTriggerLogEntity> implements SafeTriggerLogService {
    private final SafeTriggerLogMapper mapper;
    private final StringEncryptor encryptor;
    private final ContextHolderUtils contextHolderUtils;


    @Override
    public void save(SafeTriggerLogArgs args) throws RuntimeException {
        SafeTriggerLogEntity entity = ConvertUtil.map(args, SafeTriggerLogEntity.class);
        boolean result = super.save(entity);
        if (!result) {
            throw new ActionException(ResultCode.DATABASE_INSER_ERROR);
        }
    }


    @Override
    public IPage<SafeTriggerLogPageDto> page(SafeTriggerLogPageArgs args) throws RuntimeException {
        args.setIdcardEncry(encryptor.encrypt(args.getKeyword()));
        args.setOrgId(contextHolderUtils.getCurrentOrgId());
        return mapper.page(args.getQueryPage(), args);
    }
}